﻿#if NETSTANDARD2_0 || NETCOREAPP3_0
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Specialized;

namespace Longbow.Logging
{
    /// <summary>
    /// ILoggerProvider 数据库日志提供者实现类
    /// </summary>
    public class DBLoggerProvider : ILoggerProvider
    {
        private readonly Action<Exception, NameValueCollection> _action;
        private readonly IServiceCollection _services;

        /// <summary>
        /// 默认构造函数
        /// </summary>
        /// <param name="services">IServiceCollection 实例</param>
        /// <param name="action">异常记录操作回调函数</param>
        public DBLoggerProvider(IServiceCollection services, Action<Exception, NameValueCollection> action)
        {
            _services = services;
            _action = action;
        }

        /// <summary>
        /// 创建 ILogger 实例
        /// </summary>
        /// <param name="categoryName">分类名称</param>
        /// <returns>ILogger 实例</returns>
        public ILogger CreateLogger(string categoryName)
        {
            return new DBLogger(_services, _action);
        }

        /// <summary>
        /// Dispose
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
            }
        }

        // This code added to correctly implement the disposable pattern.
        /// <summary>
        /// Dispose 方法
        /// </summary>
        public void Dispose()
        {
            // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
#endif
